<template>
    <div class="department">
        <el-card class="!border-none" shadow="never">
            <el-form ref="formRef" class="mb-[-16px]" :model="queryParams" :inline="true">
                <el-form-item label="标题" prop="title">
                    <el-input class="w-[280px]" v-model="queryParams.title" clearable @keyup.enter="getLists" />
                </el-form-item>
                <el-form-item label="状态" prop="status" class="w-[280px]">
                    <el-select v-model="queryParams.status">
                        <el-option label="全部" value />
                        <el-option label="正常" value="1" />
                        <el-option label="停用" value="0" />
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none mt-4" shadow="never">
            <div>
                <el-button v-perms="['study.study/add']" type="primary" @click="handleAdd()">
                    <template #icon>
                        <icon name="el-icon-Plus" />
                    </template>
                    新增
                </el-button>
            </div>
            <el-table ref="tableRef" class="mt-4" size="large" v-loading="pager.loading" :data="pager.lists"
                row-key="id" :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
                <el-table-column label="标题" prop="title" min-width="150" show-overflow-tooltip />
                <el-table-column label="类型" prop="type_desc" min-width="150" show-overflow-tooltip />
                <el-table-column label="图片" prop="picture" min-width="100">
                    <template #default="{ row }">
                        <image-contain
                            v-if="row.picture"
                            :src="row.picture"
                            :width="60"
                            :height="45"
                            :preview-src-list="[row.picture]"
                            preview-teleported
                            fit="contain"
                        />
                    </template>
                </el-table-column>
                <el-table-column label="时长" prop="duration" min-width="150" show-overflow-tooltip />
                <el-table-column label="积分" prop="integral" min-width="150" show-overflow-tooltip />
                <el-table-column label="状态" prop="status_desc" min-width="100">
                    <template #default="{ row }">
                        <el-switch
                            :disabled="!hasPermission(['study.study/updateStatus'])"
                            v-model="row.status"
                            :active-value="1"
                            :inactive-value="0"
                            @change="changeStatus($event, row.id)"
                        />
                    </template>
                </el-table-column>
               
                <el-table-column label="创建时间" prop="create_time" min-width="180" />
                <el-table-column label="操作" width="160" fixed="right">
                    <template #default="{ row }">
                        <el-button
                            v-perms="['study.study/detail']"
                            type="primary"
                            link
                        >
                            <router-link
                                :to="{
                                    path: getRoutePath('study.study/detail'),
                                    query: {
                                        id: row.id
                                    }
                                }"
                            >
                                详情
                            </router-link>
                        </el-button>
                        <el-button v-perms="['study.study/edit']" type="primary" link @click="handleEdit(row)">
                            编辑
                        </el-button>
                        <el-button v-perms="['study.study/delete']" type="danger" link @click="handleDelete(row.id)">
                            删除
                        </el-button>
                        
                    </template>
                </el-table-column>
            </el-table>
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>

        </el-card>
        <edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false" />
    </div>
</template>
<script lang="ts" setup name="study">
import { studyLists, studyDelete ,studyStatus } from '@/api/study'
import { usePaging } from '@/hooks/usePaging'
import feedback from '@/utils/feedback'
import { getRoutePath } from '@/router'
import { hasPermission } from '@/utils/perm'

import EditPopup from './edit.vue'

const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const showEdit = ref(false)
const queryParams = reactive({
    title: '',
    status: ''
})

const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: studyLists,
    params: queryParams
})

const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}

const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.getDetail(data)
}

const handleDelete = async (id: number) => {
    await feedback.confirm('确定要删除？')
    await studyDelete({ id })
    getLists()
}

const changeStatus = async (status: any, id: number) => {
    try {
        await studyStatus({ id, status })
        getLists()
    } catch (error) {
        getLists()
    }
}

getLists()


</script>
